package com.example.notes;

import java.text.DateFormat;
import java.util.Date;

import notes.NotesDatabase;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
//import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AddNoteActivity extends Activity {
	
	private String uriPath = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_add_note);
		
		final TextView title = (TextView) findViewById(R.id.noteTitleText);
		final TextView text = (TextView) findViewById(R.id.noteText);
		
		Button okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String currentDateTimeString = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
				if(title.getText().toString().length() >= 1 
						&& NotesDatabase.addNote(title.getText().toString(), 
								text.getText().toString(),
								currentDateTimeString, uriPath)) {
					showToast("Note created!");

					finish();
				}
				else {
					showToast("Note could not be created!\nPlease add a Title!");
				}
				
			}
		});
		
		Button addPictureButton = (Button) findViewById(R.id.pictureButton);
		addPictureButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent pickPhoto = new Intent(Intent.ACTION_PICK,
				           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(pickPhoto , 1);
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private void showToast(String text) {
		Toast toast = Toast.makeText(AddNoteActivity.this, text, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent imageReturnedIntent) {
		super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
		switch (requestCode) {
		case 0:
			if (resultCode == RESULT_OK) {
				Uri selectedImage = imageReturnedIntent.getData();
				uriPath = getRealPathFromURI(this, selectedImage);
			}

			break;
		case 1:
			if (resultCode == RESULT_OK) {
				Uri selectedImage = imageReturnedIntent.getData();
				uriPath = getRealPathFromURI(this, selectedImage);
			}
			break;
		}
	}
	
	public String getRealPathFromURI(Context context, Uri contentUri) {
		  Cursor cursor = null;
		  try { 
		    String[] proj = { MediaStore.Images.Media.DATA };
		    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
		    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		    cursor.moveToFirst();
		    return cursor.getString(column_index);
		  } finally {
		    if (cursor != null) {
		      cursor.close();
		    }
		  }
		}
}
